﻿using Domain.Config;
using Domain.Types;
using Infrastructure.AggregateBase;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Numerics;
using System.Text;

namespace Domain.Model
{
    [Table("UserInfo")]
    public class UserInfoAggregate : AggregateRoot<UserInfoAggregate, int>
    {

        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public override int ID { get; set; }
        /// <summary>
        /// 昵称
        /// </summary>
        [StringLength(255)]
        public string NickName { get; set; }
        [StringLength(255)]
        public string Phone { get; set; }
        [StringLength(255)]
        public string Pwd { get; set; }
        /// <summary>
        /// 用户等级
        /// </summary>
        public UserLevel UserLevel { get; set; }

        /// <summary>
        /// 商家等级
        /// </summary>
        public TraderLevel TraderLevel { get; set; }
        /// <summary>
        /// 上级ID
        /// </summary>
        public int SuperorID { get; set; }
        /// <summary>
        /// 上级路径
        /// </summary>
        [StringLength(1000)]
        public string SuperorPath { get; set; }

        public void SetSuperor(UserInfoAggregate superorUser)
        {
            if (superorUser == null) return;

            this.SuperorID = superorUser.ID;
            this.SuperorPath = !string.IsNullOrWhiteSpace(superorUser.SuperorPath) ? superorUser.SuperorPath + "." + superorUser.ID : superorUser.ID.ToString();
        }

        /// <summary>
        /// 用户状态
        /// </summary>
        public UserInfoStatus Status { get; set; }

        /// <summary>
        /// 任务完成数
        /// </summary>
        public int TaskCount { get; set; }

        /// <summary>
        /// 邀请码
        /// </summary>
        [StringLength(10)]
        public string InviteCode { get; set; }
        [StringLength(500)]
        public string HeadImg { get; set; }
        [StringLength(255)]
        public string WxUnionID { get; set; }
        /// <summary>
        /// 最后登录时间
        /// </summary>
        public DateTime? LastLoginTime { get; set; }

        public bool IsValidRealNameInfo { get; set; }

        /// <summary>
        /// 用户升级记录
        /// </summary>
        [ForeignKey("Idx_UserLevelID")]
        public virtual IList<UserLevelLog> UserLevelLogs { get; set; }
        /// <summary>
        /// 商家升级记录
        /// </summary>
        [ForeignKey("Idx_TraderLevelID")]
        public virtual IList<TraderLevelLog> TraderLevelLogs { get; set; }
        /// <summary>
        /// 用户道具
        /// </summary>
        public virtual IList<UserProp> UserProps { get; set; }


        public virtual RealNameInfo RealNameInfo { get; set; }
        [StringLength(255)]
        public string WxOpenID { get; set; }
        [StringLength(65535)]
        public string WxData { get; set; }


        public double GetTaskCommissionPer()
        {
            double uTaskPer = GetTraderLevelConfig<int>("完成任务收益") >= GetLevelConfig<int>("完成任务收益") ?
                GetTraderLevelConfig<int>("完成任务收益"): GetLevelConfig<int>("完成任务收益");
            var systemPer= SystemConfig.GetConfigByLevelName<int>(UserLevel.ToString(), "佣金配置.任务预留佣金百分比");

            return (100 - systemPer) * ((double)uTaskPer) / 10000;
        }
        /// <summary>
        /// 获取用户等级配置信息
        /// </summary>
        public T GetLevelConfig<T>(string Key)
        {
            return SystemConfig.GetConfigByLevelName<T>(UserLevel.ToString(), Key);
        }

        public T GetTraderLevelConfig<T>(string Key)
        {
            return SystemConfig.GetConfigByLevelName<T>(TraderLevel.ToString(), Key);
        }
        /// <summary>
        /// 是否实名信息
        /// </summary>
        /// <returns></returns>
        public bool IsValidRealName()
        {
            return IsValidRealNameInfo;
        }
    }
}
